package com.imooc.sellspringcloud.server.example;


import com.imooc.sellspringcloud.server.VO.ResultVO;
import com.imooc.sellspringcloud.server.utils.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author
 * @create 2019-07-28 10:57
 **/
@RestController
@Slf4j
public class RestTemplateExample {

    @Autowired
    LoadBalancerClient loadBalancerClient;

    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/msg")
    public ResultVO getMsg() {
        // 第一种方式
 /*        RestTemplate restTemplate = new RestTemplate();
       ResponseEntity<String> entity = restTemplate.getForEntity("http://localhost:8080/msg", String.class);
        log.info("result is {}", entity.getBody());
        return ResultVOUtil.success(entity.getBody());*/
        // 第二种方式
/*      RestTemplate restTemplate = new RestTemplate();
        ServiceInstance product = loadBalancerClient.choose("PRODUCT");
        String url = String.format("http://%s:%s", product.getHost(), product.getPort()) + "/msg";

        ResponseEntity<String> entity = restTemplate.getForEntity(url, String.class);
        log.info("result is {}", entity.getBody());
        return ResultVOUtil.success(entity.getBody());*/

        // 第三种方式
        String entity = restTemplate.getForObject("http://PRODUCT/msg",
                String.class);
        log.info("result is {}", entity);
        return ResultVOUtil.success(entity);
    }


}